package com.hd.service.impl;

import com.hd.controller.vo.Node;
import com.hd.mapper.MenuMapper;
import com.hd.mapper.PermissionMapper;
import com.hd.mapper.UserMapper;
import com.hd.pojo.Menu;
import com.hd.pojo.User;
import com.hd.service.PermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by 1 on 2017/11/6.
 */
@Service("permissionService")
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    public UserMapper userMapper;

    @Override
    @Transactional(readOnly = true)
    public List<Node> searchNodes4Tree(Integer roleId) {
        List<Integer> menuList = permissionMapper.searchMenuByRoleId(roleId);
        List<Node> nodes01 = new ArrayList<Node>();
        List<Menu> menus01 = menuMapper.searchLevel01();
        for (Menu menu01:menus01) {
            Node node01 = new Node();
            node01.setIcon(menu01.getIcon_skin());
            node01.setId(menu01.getMuId());
            node01.setText(menu01.getName());
            if(menuList.contains(node01.getId())){
                node01.getState().setChecked(true);
            }
            List<Menu> menus02 = menu01.getChildren();
            if(menus02!=null && menus02.size()>0){
                for(Menu menu02:menus02){
                    Node node02 = new Node();
                    node02.setIcon(menu02.getIcon_skin());
                    node02.setId(menu02.getMuId());
                    node02.setText(menu02.getName());
                    node02.setNodes(null);
                    if(menuList.contains(node02.getId())){
                        node02.getState().setChecked(true);
                    }
                    node01.getNodes().add(node02);
                }
            }
            nodes01.add(node01);
        }
        return nodes01;
    }

    @Override
    public void grant(Integer roleId, Integer[] menuIds) {
        permissionMapper.delete(roleId);
        if(menuIds!=null&&menuIds.length>0){
            for(Integer menuId : menuIds){
                permissionMapper.add(roleId,menuId);
            }
        }
    }

    @Override
    public Map<Menu, List<Menu>> searchMenus(Integer userId) {
        Map<Menu,List<Menu>> map = new TreeMap<>();
        User user = userMapper.get(userId);
        Integer roleId = user.getRoleId();
        List<Integer> menus = permissionMapper.searchMenuByRoleId(roleId);
        for(Integer id01 : menus){
            Menu menu01 = menuMapper.get(id01);
            List<Menu> menu02 = new ArrayList<>();
            if(menu01.getpId()==null){
                map.put(menu01,menu02);
                for(Integer id02 : menus){
                    Menu menu2 = menuMapper.get(id02);
                    if(menu2.getpId()==menu01.getMuId()){
                        menu02.add(menu2);
                    }
                }
            }
        }
        return map;
    }
}
